package tv.utils {
	import flash.events.ContextMenuEvent;
	import flash.net.URLRequest;
	import flash.ui.ContextMenu ;
	import flash.ui.ContextMenuItem;
	import flash.net.navigateToURL;
	
	import flash.external.ExternalInterface;
	
	CONFIG::LOGGING {
		import org.mangui.hls.utils.Log;
	}


	public class tvContextMenu {
		
		public function tvContextMenu() {
			_contextMenu = new ContextMenu;
			_hideItemMenu();
			_addItemMenu("VTV Player...");
		}

		private var _contextMenu:ContextMenu;
		
		public function get contextMenu():ContextMenu {
			return _contextMenu;
		}
		
		private function _addItemMenu(item:String = "CoolHunter"):void {
			var _itemMenu:ContextMenuItem = new ContextMenuItem(item);
			_itemMenu.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, _onHandleEvent);
			_contextMenu.customItems.push(_itemMenu);
		}
		
		private function _onHandleEvent(evt:ContextMenuEvent):void {		
			//try {
				var browser: String = getUserAgent();
				CONFIG::LOGGING {
					Log.soha(browser);
				}
				if (browser == "Firefox") {
					openWindow("http://vtv.vn/");
				} else {
					navigateToURL(new URLRequest("http://vtv.vn/"),"_top");
				}
			//} catch (err:Error) {
				
			//}
		}
		
		private function _hideItemMenu():void {
			_contextMenu.hideBuiltInItems();
		}
		
		private function getUserAgent():String
        {
			var browser:String = "[Unknown Browser]";
            try
            {
                var userAgent:String = ExternalInterface.call("window.navigator.userAgent.toString");
                
                if (userAgent.indexOf("Safari") != -1)
                {
                    browser = "Safari";
                }
                if (userAgent.indexOf("Firefox") != -1)
                {
                    browser = "Firefox";
                }
                if (userAgent.indexOf("Chrome") != -1)
                {
                    browser = "Chrome";
                }
                if (userAgent.indexOf("MSIE") != -1)
                {
                    browser = "Internet Explorer";
                }
                if (userAgent.indexOf("Opera") != -1)
                {
                    browser = "Opera";
                }
            }
            catch (e:Error)
            {
                //could not access ExternalInterface in containing page
                return "[No ExternalInterface]";
            }
 
            return browser;
        }
		
		private static function openWindow(url : String, window : String = "_top", features : String = "") : void {
            var WINDOW_OPEN_FUNCTION : String = "window.open";
			ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features);
        }
	}
}